package com.weidong.mybbs.controller;

import com.weidong.mybbs.service.AuthService;
import com.weidong.mybbs.service.CaptcheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

@Controller
@RequestMapping("/public")
public class PublicController {

    @Autowired
    private CaptcheService captcheService;
    @Autowired
    private AuthService authService;

    /**
     * 获取图片验证码
     * @return
     */
    @RequestMapping(value = "/imgCaptcha.png",produces = MediaType.IMAGE_PNG_VALUE)
    public void getImgCaptcha(HttpServletRequest request,
                              HttpServletResponse response)throws IOException {
        String sessionId = authService.getSessionId(request);
        OutputStream outputStream = response.getOutputStream();
        ImageIO.write(captcheService.getImgCaptcha(sessionId),"png",outputStream);
        outputStream.flush();
        outputStream.close();
    }


}
